import sys
import time
from threading import Thread

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from ui import *
from win32com import client


class Main(QThread):
    signal = Signal(int)
    paused = False

    def __init__(self):
        super().__init__()
        self.mutex = QMutex()
        self.pauseCond = QWaitCondition()

    def run(self):
        for i in range(secs):
            time.sleep(1)
            self.mutex.lock()

            if self.paused:
                self.pauseCond.wait(self.mutex)
            self.mutex.unlock()

            self.signal.emit(i)

    def pause(self):
        self.mutex.lock()
        self.paused = True
        self.mutex.unlock()

    def resume(self):
        self.mutex.lock()
        self.paused = False
        self.pauseCond.wakeAll()
        self.mutex.unlock()


class UI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_ui.Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle("计时器")
        self.setWindowIcon(QIcon(QPixmap(":/icon.ico")))
        self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)  # 设置置顶
        self.initUI()

        self.main = Main()
        self.main.signal.connect(self.callBack)

    def initUI(self):
        self.ui.mins.valueChanged.connect(self.timeValueChangeed)
        self.ui.enterTime.valueChanged.connect(self.enterTimeChanged)

        self.ui.start.clicked.connect(self.start)
        self.ui.min.clicked.connect(self.timeClicked)
        self.ui.sec.clicked.connect(self.timeClicked)

    def speak(self, text: str):
        engine = client.Dispatch("SAPI.SpVoice")
        engine.Speak(text)

    def timeClicked(self):
        if not self.main.paused:
            self.ui.min.setStyleSheet("color: red;")
            self.ui.sec.setStyleSheet("color: red;")
            # self.falg = False
            self.main.pause()

        else:
            self.ui.min.setStyleSheet("color: green;")
            self.ui.sec.setStyleSheet("color: green;")
            # self.falg = True
            self.main.resume()

    def timeValueChangeed(self, msg: int):
        mins = self.ui.mins.value()
        self.ui.enterTime.setValue(mins)

    def enterTimeChanged(self, msg: int):
        self.ui.mins.setValue(msg)

    def start(self):
        global secs
        self.ui.min.setStyleSheet("color: green;")
        self.ui.sec.setStyleSheet("color: green;")
        self.main.terminate()  # 终止线程
        secs = self.ui.enterTime.value()*60
        self.ui.min.display(secs//60)
        self.ui.sec.display(secs % 60)
        # self.falg = True
        self.main.start()

    def callBack(self, msg: int):
        if self.ui.sec.value() == 0:
            self.ui.min.display(self.ui.min.value() - 1)
            self.ui.sec.display(59)
        else:
            self.ui.sec.display(self.ui.sec.value() - 1)
        # self.ui.showTime.display(secs - (msg + 1))
        if secs == msg + 1:
            Thread(target=self.speak, args=(f"{secs}秒计时完成",)).start()

            reply = QMessageBox.information(
                self, "时间到", f"{secs}秒 计时完成\n按<OK>键关闭程序",
                QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Close)

            if reply.name == "Close":
                sys.exit()


app = QApplication([])
ui = UI()
ui.show()
sys.exit(app.exec())
